'''
Created on 2015年12月14日

@author: SunXiaohui
'''
import re
import Config

'''
    数据结构：
    UAM：根集合，其元素代表一个用户，可以用下标来表示用户ID
    UAMList：UAM的子元素，其元素代表一个用户对某电影的评分
    userAndMovie：UAMList的子元素，内容容器
'''
# 读取文件
def readFile(filePath):
    try:
        UAM = []
        UAMList = []
        userAndMovie = {'user':0, 'movie':0,'score':0} 
        with open(filePath) as file:
            for line in file.readlines():
                line.replace("\n",'')
                if(line.find('|') != -1):
                    dataA = line.split('|')
                    userAndMovie['user'] = int(dataA[0])
                    if(UAMList != []):
                        UAM.append(UAMList)
                        UAMList.clear()                                               
                else:
                    if(line.find('  ') != -1):
                        dataB = line.split('  ')
                        userAndMovie['movie'] = int(dataB[0])
                        userAndMovie['score'] = int(dataB[1])
                        UAMList.append(userAndMovie)
                    else:
                        line = re.sub(r'\D', "", line)    
                        userAndMovie['movie'] = int(line)
                        userAndMovie['score'] = 200
                        UAMList.append(userAndMovie) 
                                        
    except FileExistsError as e:
        print('exception' , e)
    return UAM               
    